description = 'YDB LogStash plugins'

allprojects {
    group = 'tech.ydb.logstash'
    version = '0.9.6'
}

buildscript {
    repositories {
        mavenCentral()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }

    dependencies {
        classpath "gradle.plugin.com.github.johnrengelman:shadow:7.1.2"
    }

    ext.snakeYamlVersion = '1.29'
}

subprojects {
    apply plugin: 'java'
    apply from: LOGSTASH_CORE_PATH + '/../rubyUtils.gradle'
    apply plugin: 'com.github.johnrengelman.shadow'

    // ===========================================================================
    // plugin info
    // ===========================================================================
    pluginInfo.licenses        = ['Apache-2.0'] // list of SPDX license IDs
    pluginInfo.longDescription = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using \$LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"
    pluginInfo.authors         = ['Mike Lukashev', 'Sergey Provolovich', 'Alexandr Gorshenin']
    pluginInfo.email           = ['alexadr268@ydb.tech']
    pluginInfo.homepage        = "https://ydb.tech/"
    // ===========================================================================

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    repositories {
        mavenCentral()
    }

    shadowJar {
        archiveClassifier = null
    }

    dependencies {
        implementation fileTree(dir: LOGSTASH_CORE_PATH, include: "**/logstash-core.jar")

        testImplementation group: 'org.jruby',                name: 'jruby-complete',   version: '9.2.11.0'
        testRuntimeOnly    group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.21.1'

        testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
    }

    clean {
        delete "${projectDir}/Gemfile"
        delete "${projectDir}/lib/"
        delete "${projectDir}/vendor/"
        delete "${projectDir}/VERSION"

        fileTree(dir: projectDir, includes: ["*.gemspec"]).each { file ->
            file.delete()
        }

        fileTree(dir: projectDir, includes: ["*.gem"]).each { file ->
            file.delete()
        }
    }

    test {
        // Enable JUnit 5 (Gradle 4.6+).
        useJUnitPlatform()

        // Hide warning from JRuby 9.2
        jvmArgs '--add-opens', 'java.base/sun.nio.ch=ALL-UNNAMED'

        // Always run tests, even when nothing changed.
        dependsOn 'cleanTest'

        // Pass special tests parameters
        if (project.hasProperty('YDB_ENDPOINT')) {
            systemProperty 'YDB_ENDPOINT', project.getProperty('YDB_ENDPOINT')
        }
        if (project.hasProperty('YDB_DATABASE')) {
            systemProperty 'YDB_DATABASE', project.getProperty('YDB_DATABASE')
        }

        // Show test results.
        testLogging {
            exceptionFormat org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
        }
    }

    tasks.withType(JavaCompile) {
        options.encoding = 'UTF-8'
    }

    tasks.register("vendor"){
        dependsOn shadowJar
        doLast {
            String vendorPathPrefix = "vendor/jar-dependencies"
            String projectGroupPath = project.group.replaceAll('\\.', '/')
            File projectJarFile = file("${vendorPathPrefix}/${projectGroupPath}/${pluginInfo.pluginFullName()}/${project.version}/${pluginInfo.pluginFullName()}-${project.version}.jar")
            projectJarFile.mkdirs()
            java.nio.file.Files.copy(
                file("$buildDir/libs/${project.name}-${project.version}.jar").toPath(),
                projectJarFile.toPath(),
                java.nio.file.StandardCopyOption.REPLACE_EXISTING
            )
            validatePluginJar(projectJarFile, project.group)
        }
    }

    tasks.register("generateRubySupportFiles") {
        doLast {
            generateRubySupportFilesForPlugin(project.description, project.group, version)
        }
    }

    tasks.register("removeObsoleteJars") {
        doLast {
            new FileNameFinder().getFileNames(
                    projectDir.toString(),
                    "vendor/**/" + pluginInfo.pluginFullName() + "*.jar",
                    "vendor/**/" + pluginInfo.pluginFullName() + "-" + version + ".jar").each { f ->
                delete f
            }
        }
    }

    tasks.register("createVersionFile") {
        doLast {
            def versionFile = file("${projectDir}/VERSION")
            versionFile.text = version
        }
    }


    tasks.register("gem") {
        dependsOn = [createVersionFile, downloadAndInstallJRuby, removeObsoleteJars, vendor, generateRubySupportFiles]
        doLast {
            buildGem(projectDir, buildDir, pluginInfo.pluginFullName() + ".gemspec")
        }
    }
}
